package com.example.myapplication.ui.fragment

import android.content.Context
import android.graphics.Color
import android.os.Build
import android.view.View
import androidx.annotation.RequiresApi
import androidx.lifecycle.Observer
import com.example.myapplication.R
import com.example.myapplication.bean.category.CategoryBean
import com.example.myapplication.ui.fragment.project.ProjectArticleFragment
import com.kun.base.adapter.MyFragmentPagerAdapter
import com.kun.base.ui.AbsBaseFragment
import kotlinx.android.synthetic.main.fragment_project.*
import kotlinx.android.synthetic.main.toolbar_layout.*
import net.lucode.hackware.magicindicator.ViewPagerHelper
import net.lucode.hackware.magicindicator.buildins.commonnavigator.CommonNavigator
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.CommonNavigatorAdapter
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.IPagerIndicator
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.IPagerTitleView
import net.lucode.hackware.magicindicator.buildins.commonnavigator.indicators.LinePagerIndicator
import net.lucode.hackware.magicindicator.buildins.commonnavigator.titles.ClipPagerTitleView
import net.lucode.hackware.magicindicator.buildins.commonnavigator.titles.SimplePagerTitleView

/**
 *项目的fragment
 *
 * @author : Kun
 * @date : 2021/6/16
 * @time : 10:45
 */
class ProjectFragment : AbsBaseFragment() {
    private lateinit var projectViewModel: ProjectViewModel

    private lateinit var adapter: MyFragmentPagerAdapter

    private lateinit var fragments: ArrayList<AbsBaseFragment>
    private lateinit var titles: ArrayList<String>

    override fun getLayoutId(): Int {
        return R.layout.fragment_project
    }

    override fun initViewModel() {
        projectViewModel = getFragmentScopeViewModel(ProjectViewModel::class.java)
        projectViewModel.projectRequest.projectTreeResult.observe(this, Observer { t ->

            if (t.errorCode == 0) {
                initData(t.data!!)
            }
        })
    }

    override fun initView() {
        iv_back.visibility = View.GONE
        tv_title.text = getString(R.string.project)

        projectViewModel.requestProjectTreeList()
    }

    private fun initData(list: MutableList<CategoryBean>) {
        fragments = ArrayList()
        titles = ArrayList()
        for (categoryBean in list) {
            fragments.add(ProjectArticleFragment.getInstance(categoryBean))
            titles.add(categoryBean.name)
        }

        adapter = MyFragmentPagerAdapter(childFragmentManager, fragments, titles)
        vp_project.adapter = adapter

        initMagicIndicator()
    }

    private fun initMagicIndicator() {
        val commonNavigator = CommonNavigator(activity)
        commonNavigator.adapter = object : CommonNavigatorAdapter() {
            @RequiresApi(Build.VERSION_CODES.M)
            override fun getTitleView(context: Context?, index: Int): IPagerTitleView {

                val simplePagerTitleView = SimplePagerTitleView(context)
                simplePagerTitleView.text = titles[index]
                simplePagerTitleView.selectedColor = activity!!.getColor(R.color.c_666666)
                simplePagerTitleView.normalColor = activity!!.getColor(R.color.c_999999)

                simplePagerTitleView.setOnClickListener {
                    vp_project.currentItem = index
                }

                return simplePagerTitleView
            }

            override fun getCount(): Int {
                return fragments.size
            }

            override fun getIndicator(context: Context?): IPagerIndicator {
                return LinePagerIndicator(context).apply {
                    mode = LinePagerIndicator.MODE_WRAP_CONTENT
                    setColors(Color.parseColor("#3587f2"))
                }
            }

        }
        mi_project.navigator = commonNavigator
        ViewPagerHelper.bind(mi_project, vp_project)
    }
}